﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Master_Purchase_Rule : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLMasterPurchaseRule bll = new MasterPurchaseRuleBLL();
        IBLLTree bllTree = new TreeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
            }
        }

        public void LoadDataGrid()
        {
            IList<MasterPurchaseRuleInfo> info = bll.GetMasterPurchaseRule();
            Grid.DataSource = info;
            Grid.DataBind();

            cmdFac.GridCommand(panelList, panelGrid, panelCommand);
        }

        public void LoadAllBgmSourceTree()
        {
            IList<TreeInfo> info = bllTree.GetAllBgmSourceTree();
            SourceTree.CheckBoxes = true;
            SourceTree.DataSource = info;
            SourceTree.DataBind();
            SourceTree.ExpandAllNodes();
        }

        public void LoadBgmSourceTreeWithRule(int rule_seq, bool show_checkbox)
        {
            IList<TreeInfo> info = bllTree.GetBgmSourceTreeWithRule(rule_seq);
            SourceTree.CheckBoxes = show_checkbox;
            SourceTree.DataSource = info;
            SourceTree.DataBind();
            SourceTree.ExpandAllNodes();

            if (show_checkbox == false)
            {
                foreach (RadTreeNode node in SourceTree.GetAllNodes())
                {
                    if (node.Checked == false)
                    {
                        node.Remove();
                    }
                }
            }
        }

        public void GetKeyValue(string seq)
        {
            if (seq.Length > 0)
            {
                MasterPurchaseRuleInfo info = bll.GetMasterPurchaseRule(Convert.ToInt32(seq));

                txtName.Text = info.Name;
                txtDescr.Text = info.Descr;
                LoadBgmSourceTreeWithRule(Convert.ToInt32(seq), false);

                cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
            }
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList, panelGrid, panelCommand);
            LoadAllBgmSourceTree();
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            cmdFac.EditCommand(panelList, panelGrid, panelCommand);
            LoadBgmSourceTreeWithRule(Convert.ToInt32(hdSeq.Value), true);
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            IList<MasterPurchaseRuleInfo> infoList = new List<MasterPurchaseRuleInfo>();
            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    MasterPurchaseRuleInfo info = new MasterPurchaseRuleInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                    infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                    infoRecord.Updated_when = DateTime.Now;
                    info.InfoRecord = infoRecord;
                    infoList.Add(info);
                }
            }
            else
            {
                MasterPurchaseRuleInfo info = new MasterPurchaseRuleInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(hdSeq.Value);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bll.DeleteAll(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadDataGrid();
        }

        protected void AddNewSave_Click(object sender, EventArgs e)
        {
            MasterPurchaseRuleInfo info = new MasterPurchaseRuleInfo();
            IList<MasterPurchaseRuleMappingInfo> infoList = new List<MasterPurchaseRuleMappingInfo>();
            RecordInfo infoRecord = new RecordInfo();
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoRecord = infoRecord;

            foreach (RadTreeNode node in SourceTree.GetAllNodes())
            {
                if (node.Checked && node.Nodes.Count == 0)
                {
                    string[] all_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);
                    MasterPurchaseRuleMappingInfo infoMapping = new MasterPurchaseRuleMappingInfo();
                    BgmSourceInfo infoSource = new BgmSourceInfo();
                    infoSource.Seq = Convert.ToInt32(all_value[1]);
                    infoMapping.InfoSource = infoSource;
                    infoMapping.InfoRecord = infoRecord;
                    infoList.Add(infoMapping);
                }
            }

            if (ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                bll.Insert(info, infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
            LoadDataGrid();
        }

        protected void EditSave_Click(object sender, EventArgs e)
        {
            MasterPurchaseRuleInfo info = new MasterPurchaseRuleInfo();
            IList<MasterPurchaseRuleMappingInfo> infoList = new List<MasterPurchaseRuleMappingInfo>();
            RecordInfo infoRecord = new RecordInfo();
            info.Seq = Convert.ToInt32(hdSeq.Value);
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
            infoRecord.Updated_when = DateTime.Now;
            info.InfoRecord = infoRecord;

            foreach (RadTreeNode node in SourceTree.GetAllNodes())
            {
                if (node.Checked && node.Nodes.Count == 0)
                {
                    string[] all_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);
                    MasterPurchaseRuleMappingInfo infoMapping = new MasterPurchaseRuleMappingInfo();
                    BgmSourceInfo infoSource = new BgmSourceInfo();
                    infoSource.Seq = Convert.ToInt32(all_value[1]);
                    infoMapping.InfoSource = infoSource;
                    infoMapping.InfoRecord = infoRecord;
                    infoList.Add(infoMapping);
                }
            }

            if (ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                bll.Update(info, infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
            cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
            LoadBgmSourceTreeWithRule(Convert.ToInt32(hdSeq.Value), false);
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            cmdFac.CancelCommand(panelList, panelGrid, panelCommand);
            LoadBgmSourceTreeWithRule(Convert.ToInt32(hdSeq.Value), false);
        }

        protected void ShowGrid_Click(object sender, EventArgs e)
        {
            LoadDataGrid();
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadDataGrid();
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            GetKeyValue(e.Argument);
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }

        protected void SourceTree_NodeDataBound(object sender, RadTreeNodeEventArgs e)
        {
            e.Node.Checked = ((e.Node.DataItem as TreeInfo).Check == 1);
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid.MasterTableView.FilterExpression = string.Empty;
            LoadDataGrid();
        }
    }
}
